<?php

function pay_callback(){
        
        $special_uid = "1798928597";
        
        // 准备输出数组的变量名
        $data = array('content' => array());
        
        // 验证facebook签名
        $allinfo = $this->parse_signed_request($_REQUEST['signed_request'], $GLOBALS['platform_config']['secret']);
        
        if ($allinfo == null) {
            
            $error = "singe error :".json_encode($_REQUEST).'pay_error';
            M_Response::errorJson($error);
        }
                
        $credits     = $allinfo['credits'];
        $uid         = $allinfo['user_id'];
        $func         = $_REQUEST['method'];
        $status        = isset($_REQUEST['status'])? $_REQUEST['status'] : 0;
        $order_id    = $_REQUEST['order_id'];
        
        if ( $func == 'payments_status_update' and ($status == 'placed')) {
        
            //第二次回调，
            //1,记录 订单，数据库和log文件
            $order_details = json_decode($credits['order_details'],true);
            $extend_idfo   = json_decode($order_details['items'][0]['data'],true);
            
            $server_sid = $extend_idfo['serverid'];
            $gold        = $extend_idfo['golds'];
            $fb_price   = $order_details['items'][0]['price'];
            $uid          = $order_details['receiver'];
            
            //保存订单
            $pay_model = new PayModel();
            $pay_data = array();
            $pay_data['token'] = "1";
            $pay_data['item'] = empty($extend_idfo['title'])?'XZ100': $extend_idfo['title'];
            $pay_data['credits'] = $fb_price;
            $pay_data['num'] = 1;
            $pay_data['billno'] = $order_id;
                        
            $pay_data['paytime'] = date('Y-m-d H:i:s',time());;
            $pay_data['paycredits'] = $fb_price;
                        
            if (!$pay_model->newOrderQQ($uid,$pay_data)){
                
                $of = fopen('pay_error_log.txt','w');
                if($of){
                    fwrite($of,json_encode($pay_model->_error));
                }
                fclose($of);
                
                M_Response::errorJson($pay_model->_error);
            }
            
            //if($uid == $special_uid){
                $data['content']['status'] = 'settled';
            //}
            
        }else if ( $func == 'payments_status_update' ) {
            
            $order_details = json_decode($credits['order_details'],true);
            $order_details = self::objToArray($order_details);
            $extend_idfo   = json_decode($order_details['items'][0]['data'],true);
            //{"order_id":9014637684809,"buyer":100000434450971,"app":130121980504560,"receiver":100000434450971,"currency":"FBZ","amount":1,"time_placed":1365848044,"properties":1,"update_time":1365848051,"data":"","ref_order_id":0,"items":[{"item_id":"0","title":"100 XZ100","description":"100 XZ100 \u8d2d\u4e70\u52cb\u7ae0","image_url":"http:\/\/lotr.oasgames.com\/fbapp\/resource\/images\/gold.png","product_url":"http:\/\/lotr.oasgames.com\/fbapp\/resource\/images\/gold.png","price":1,"data":"{\"golds\":100}"}],"status":"settled"}
            $gold = empty($extend_idfo['golds'])?10:$extend_idfo['golds'];
            
            $order_detail_data   = json_decode($order_details['data'],true);
            $review_status = isset($order_detail_data['review_status']) ?  $order_detail_data['review_status'] : "";
            $fb_price   = $order_details['items'][0]['price'];
            
            //第三次回调，确认状态给用户充值
            //1,记录 订单，数据库和log文件，
            //2,记录调用游戏支付接口的url调用过程为log文本
            if ($status == 'settled'){
                $tt = time();
                $data = $this->goods($uid, $order_id, "", "", $gold, $tt, '');
            } else {
                //$pay_handler->updateFb_order_state($order_id,$status) and $data['content']['order_id'] = $order_id;
            }
        }else if ($func == 'payments_get_items') {
            
            //第一次回调，确认用户订单
            $order_info = stripcslashes($credits['order_info']);
            $item         = json_decode($order_info, true);
            $item['data']= json_encode($item['data']);
            $data['content'] = array($item);
            
        }else {
            exit("func error :".json_encode($_REQUEST).'pay_error');
        }
        
        $data['method'] = $func;
        ob_end_clean();
        echo json_encode($data);
        exit;
    }